const char *help = "\
progname: fft2D.cc\n\
code2html: This program reads a pgm image and computes its FFT 2D.\n\
version: Torch3 vision2.0, 2005\n\
(c) Sebastien Marcel (marcel@idiap.ch)\n";


#include "ImageGray.h"
#include "ImageRgb.h"
#include "DiskXFile.h"
#include "CmdLine.h"
#include "TPFunctions.h"
#include "xtprobeImageDiskXFile.h"

using namespace Torch;




////////////************* PROGRAMME PRINCIPALE ******************///////////////////////

int main(int argc, char **argv)
{
	char *image_filename;
        char *file;
	bool verbose;
	int filtre;
	real seuil;


	CmdLine cmd;
	cmd.setBOption("write log", false);
  	cmd.info(help);
  	cmd.addText("\nArguments:");
  	cmd.addSCmdArg("image filename", &image_filename, "image filename");
  	cmd.addText("\nOptions:");
 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
  	cmd.addSCmdOption("-comp", &file, " ", "Fichier à comparer");
        cmd.addICmdOption("-filtre", &filtre, 1, "Numero du détecteur");
  	cmd.addRCmdOption("-seuil", &seuil, 10, "Seuil de détection");
 	cmd.read(argc, argv);

	Image *source = NULL;
        source=loadImage(verbose,image_filename);


        if(strcmp(file," ")==0)
        {

//******  filtreion de contours  ***********//

	char *name = new char[300];
        char thresh [50];
        int n;
        n=sprintf (thresh, "%.0f", seuil);
        
	switch(filtre)
        {
	  case 1:
             strcpy(name, setTitle(image_filename,(char*)"-Sobel-"));
             strcat(name, thresh);
             strcat(name, (char*)"-");
	     sobelDetect(source,verbose,seuil,name);
             break; 

	  case 2: 
             strcpy(name, setTitle(image_filename,(char*)"-Canny-"));
             strcat(name, thresh);
             strcat(name, (char*)"-");
	     cannyDetect(source,verbose,seuil,name);
             break; 

	  case 3: 
             strcpy(name, setTitle(image_filename,(char*)"-Prewitt-"));
             strcat(name, thresh);
             strcat(name, (char*)"-");
	     filtreDetect(source,verbose,seuil,name,3);
             break;

	  case 4:
             strcpy(name, setTitle(image_filename,(char*)"-Kirsch-"));
             strcat(name, thresh);
             strcat(name, (char*)"-");
	     filtreDetect(source,verbose,seuil,name,15);
             break;
        }
        }
        else
        {
        //******  Comparaison de détecteurs  ***********//
        if(!loadImage(false,file))
        {
        print("\n   Le fichier %d est introuvable !!!\n\n", file);
        }
        else
        {

        Image *image_1 = NULL;
        image_1=loadImage(false,file);

              //    Vérification de la taille des images
        if ((image_1->width != source->width) || (image_1->height != source->height))
        {
            print("\n   Erreur: Veuillez vérifier la taille des images !!!\n\n");
            return -1;
        }

	int nb_reference=nbrePixels(image_1,0);

        int nb_gradient=nbrePixels(source,255);

        int nb_commun=nbreCommun(image_1,source);

        real precision = (real)nb_commun / nb_gradient;
        if(precision > 1.0) precision = 1.0;

        real rappel = (real)nb_commun/nb_reference;
        if(rappel > 1.0) rappel = 1.0;
        
        real f_mesure = (real)2*(precision*rappel)/(precision+rappel);
        if(f_mesure > 1.0) f_mesure = 1.0;
        
        	print("   RESULSTATS:\n");
		print("        Nombre référence = %d\n", nb_reference);
		print("        Nombre gradient  = %d\n", nb_gradient);
                print("        Nombre commun    = %d\n", nb_commun);
		print("        Valeur précision = %.5f\n", precision);
		print("        Valeur rappel    = %.5f\n", rappel);
                print("        Valeur f-mesure  = %.5f\n", f_mesure);

        }
        }
	delete source;

	return(0);
}
